package ycl.base.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import ycl.base.entity.Files;
import ycl.base.req.FilesReq;
import ycl.base.res.FilesRes;
import ycl.base.service.FilesService;
import ycl.common.result.Result;
import ycl.common.base.controller.BaseController;
import ycl.common.result.REnum;
import ycl.common.enums.FileTypeEnum;
import ycl.common.exception.CustomException;
import ycl.common.models.base.A;
import ycl.common.models.req.base.ObjReq;
import ycl.common.models.res.hovel.UploadRes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ycl
 * @since 2022-06-30 18:33:18
 */
@RestController
@RequestMapping("/files")
@Api(tags = "文件上传")
public class FilesController extends BaseController<Files, FilesReq, FilesRes> {

	@Resource
	private FilesService filesService;

	@Override
	@ApiOperation(value = "列表分页", hidden = true)
	public Result<Page<FilesRes>> pageList(HttpServletRequest request, Files files) {
		return Result.success(filesService.pageList(request, files, FilesRes.class));
	}

	@Override
	@ApiOperation(value = "保存", hidden = true)
	public <E extends FilesReq> Result<A> save(HttpServletRequest request, E e) {
		throw new CustomException(REnum.CUSTOM_SERVER_REJECT);
	}

	@Override
	@ApiOperation(value = "修改", hidden = true)
	public <E extends FilesReq> Result<A> update(HttpServletRequest request, E e) {
		throw new CustomException(REnum.CUSTOM_SERVER_REJECT);
	}

	@Override
	@ApiOperation(value = "根据id批量删除", hidden = true)
	public Result<Files> deleteBatchIds(HttpServletRequest request, @RequestBody @Valid ObjReq<List<Long>> req) {
		throw new CustomException(REnum.CUSTOM_SERVER_REJECT);
	}

	@PostMapping("/music")
	@ApiOperation("音乐")
	@ApiOperationSupport(order = 11)
	public Result<UploadRes> music(HttpServletRequest request, MultipartFile file) {
		UploadRes upload = filesService.upload(request, file, FileTypeEnum.AUDIO);
		return Result.success(upload);
	}


	@PostMapping("/video")
	@ApiOperation("视频")
	@ApiOperationSupport(order = 12)
	public Result<UploadRes> video(HttpServletRequest request, MultipartFile file) {
		UploadRes upload = filesService.upload(request, file, FileTypeEnum.VIDEO);
		return Result.success(upload);
	}
}
